import QtQuick
import QtQml
import QtQuick.Controls

import "../as_qml"

Item {
    width: 640
    height: 480
    visible: true

    property color mTextColor: "#1a1c1e"

    function setTipsShow(flag, text) {
        tipsText.color = (flag === true) ? "green" : "red"
        tipsText.text = qsTr(text)
    }

    Timer {
        id: tim
        interval: 33
        running: false
        repeat: true

        onTriggered: {
            cameraImage.source = ""
            cameraImage.source = "image://camera/1" // "image://camera"会触发myimageprovider的一个回调函数
        }
    }

    Connections {
        target: myHandle

        function onPhotoFileDirectoryOpened(filePath) {
            photoPathText.text = filePath + "/";
        }

        function onXmlFileDirectoryOpened(filePath) {
            recognizerPathText.text = filePath
        }
    }

    Rectangle {
        width: parent.width - rightColumn.width
        anchors.top: parent.top
        anchors.bottom: tipsBottomRect.top
        color: "#404040"

        Image {
            id: cameraImage
            width: parent.width
            height: parent.height
            cache: false
        }
    }


    Column {
        id: rightColumn
        width: 200
        anchors.top: parent.top
        anchors.bottom: tipsBottomRect.top
        anchors.right: parent.right
        spacing: 4
        padding: 4

        AsComboBox {
            id: chooseCameraList
            width: parent.width - parent.padding * 2
            height: 28
            model: myHandle.getCameraNameList()
        }

        AsButton {
            id: openCamera
            width: 90
            height: 28
            text: qsTr("打开摄像头")

            onClicked: {
                var ret = myHandle.setFaceRecognizer(recognizerPathText.text, modeComboBox1.currentIndex)
                var tips = (ret === false && modeComboBox1.currentIndex !== 3) ? "加载识别器模型失败,请先选择识别器模型文件" : ""
                var ret1 = myHandle.openCamera(chooseCameraList.currentIndex)
                tips += (ret1 === false) ? "  打开摄像头失败,请选择可以被打开的摄像头设备" : ""
                if(tips === "") {
                    tips = "无"
                    setTipsShow(true, tips)
                } else setTipsShow(false, tips)
                if(ret1 === true) tim.running = true
            }
        }

        AsButton {
            id: closeCamera
            width: 90
            height: 28
            text: qsTr("关闭摄像头")

            onClicked: {
                tim.running = false
                myHandle.closeCamera()
                cameraImage.source = ""
                setTipsShow(true, "无")
            }
        }

        AsButton {
            id: takePhoto
            width: 60
            height: 28
            text: qsTr("抓拍")

            onClicked: {
                var ret = myHandle.savePhotoFile(photoPathText.text)
                setTipsShow(ret, (ret === false) ? "抓拍保存失败,请先选择要保存的路径" : "无")
            }
        }

        AsButton {
            id: openPhotoPathBtn
            width: 130
            height: 28
            text: qsTr("选择抓拍保存路径")

            onClicked: {
                var ret = myHandle.openPhotoFileDir()
                setTipsShow(ret, (ret === false) ? "操作取消" : "无")
            }
        }

        AsTextField {
            id: photoPathText
            width: parent.width - parent.padding * 2
            height: 28
            placeholderText: qsTr("请选择抓拍图片的保存路径")
            color: mTextColor
            readOnly: true
            verticalAlignment: Text.AlignVCenter
        }

        Row {
            width: parent.width
            height: 28
            spacing: 4

            AsText {
                id: modeTitleText
                width: 70
                height: parent.height
                text: qsTr("识别器模型:")
                verticalAlignment: Text.AlignVCenter
            }

            AsComboBox {
                id: modeComboBox1
                width: 80
                height: parent.height
                model: ["Eigen", "Fisher", "LBPH", "None"]
            }
        }

        AsButton {
            id: openRecognizerPathBtn
            width: 130
            height: 28
            text: qsTr("选择识别器模型文件")

            onClicked: {
                var ret = myHandle.openRecognizerFile()
                setTipsShow(ret, (ret === false) ? "操作取消" : "无")
            }
        }

        AsTextField {
            id: recognizerPathText
            width: parent.width - parent.padding * 2
            height: 28
            placeholderText: qsTr("请选择识别器模型文件")
            color: mTextColor
            readOnly: true
            verticalAlignment: Text.AlignVCenter
        }
    }

    Rectangle {
        id: tipsBottomRect
        width: parent.width
        height: 28
        anchors.bottom: parent.bottom
        color: "#e0e0e0"

        Row {
            anchors.fill: parent

            AsText {
                id: tipsTitle
                width: 40
                height: parent.height
                text: qsTr("提示:")
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
            }

            AsText {
                id: tipsText
                width: parent.width - tipsTitle.width
                height: parent.height
                text: qsTr("无")
                color: "green"
                verticalAlignment: Text.AlignVCenter
            }
        }
    }

}
